<?php
/*
  $Id$

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2010 osCommerce

  Released under the GNU General Public License
*/

  require('includes/application_top.php');

  if (!tep_session_is_registered('customer_id')) {
    $navigation->set_snapshot();
    tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
  }

// needs to be included earlier to set the success message in the messageStack
  require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_ACCOUNT_PASSWORD);

  if (isset($HTTP_POST_VARS['action']) && ($HTTP_POST_VARS['action'] == 'process') && isset($HTTP_POST_VARS['formid']) && ($HTTP_POST_VARS['formid'] == $sessiontoken)) {
    $password_current = tep_db_prepare_input($HTTP_POST_VARS['password_current']);
    $password_new = tep_db_prepare_input($HTTP_POST_VARS['password_new']);
    $password_confirmation = tep_db_prepare_input($HTTP_POST_VARS['password_confirmation']);

    $error = false;

    if (strlen($password_new) < ENTRY_PASSWORD_MIN_LENGTH) {
      $error = true;

      $messageStack->add('account_password', ENTRY_PASSWORD_NEW_ERROR);
    } elseif ($password_new != $password_confirmation) {
      $error = true;

      $messageStack->add('account_password', ENTRY_PASSWORD_NEW_ERROR_NOT_MATCHING);
    }

    if ($error == false) {
      $check_customer_query = tep_db_query("select customers_password from " . TABLE_CUSTOMERS . " where customers_id = '" . (int)$customer_id . "'");
      $check_customer = tep_db_fetch_array($check_customer_query);

      if (tep_validate_password($password_current, $check_customer['customers_password'])) {
        tep_db_query("update " . TABLE_CUSTOMERS . " set customers_password = '" . tep_encrypt_password($password_new) . "' where customers_id = '" . (int)$customer_id . "'");

        tep_db_query("update " . TABLE_CUSTOMERS_INFO . " set customers_info_date_account_last_modified = now() where customers_info_id = '" . (int)$customer_id . "'");

        $messageStack->add_session('account', SUCCESS_PASSWORD_UPDATED, 'success');

        tep_redirect(tep_href_link(FILENAME_ACCOUNT, '', 'SSL'));
      } else {
        $error = true;

        $messageStack->add('account_password', ERROR_CURRENT_PASSWORD_NOT_MATCHING);
      }
    }
  }

  $breadcrumb->add(NAVBAR_TITLE_1, tep_href_link(FILENAME_ACCOUNT, '', 'SSL'));
  $breadcrumb->add(NAVBAR_TITLE_2, tep_href_link(FILENAME_ACCOUNT_PASSWORD, '', 'SSL'));

  require(DIR_WS_INCLUDES . 'template_top.php');
  require('includes/form_check.js.php');
?>
<!--middle area start-->
<div class="middle-area">
  <div class="innermidbox fl">
    <div class="innermidbox-top"></div>
    <div class="innermidbox-mid">
      <div class="pdt5 pdl20 pdr20 pdb5 minheight527px">
        <div class="breadcrumbs"><a href="index.php">Home</a> > <span><?php echo HEADING_TITLE; ?></span></div>
        <div class="spacer10"></div>
        <div class="">
          <h1 class="pagetitle fl"><?php echo HEADING_TITLE; ?></h1>
          <a href="<?=tep_href_link(FILENAME_ACCOUNT, '', 'SSL')?>" class="backbtn fr"></a>
          <div class="clear"></div>
          <div class="hline"></div>
          <div class="spacer3"></div>
          <!--left panel start-->
          <?php include('includes/template_left.php'); ?>
          <!--left panel end-->
          <!--right panel start-->
          <div class="rightpanel mgl15 fl">
            <?php
				  if ($messageStack->size('account_password') > 0) {
					echo $messageStack->output('account_password');
				  }
			?>
            <?php echo tep_draw_form('account_password', tep_href_link(FILENAME_ACCOUNT_PASSWORD, '', 'SSL'), 'post', 'onsubmit="return check_form(account_password);"', true) . tep_draw_hidden_field('action', 'process'); ?>
            <div class="contentContainer">
              <div class="cms"> <span class="inputRequirement" style="float: right;"><?php echo FORM_REQUIRED_INFORMATION; ?></span>
                <h3><?php echo MY_PASSWORD_TITLE; ?></h3>
              </div>
              <div class="contentText">
                <div class="contact-left mgt15 w150px"><?php echo ENTRY_PASSWORD_CURRENT; ?></div>
                <div class="hinputbg fl"> <?php echo tep_draw_password_field('password_current', '', 'class="hinput"') ?> </div>
                <?php (tep_not_null(ENTRY_PASSWORD_CURRENT_TEXT) ? '<div class="required fl">' . ENTRY_PASSWORD_CURRENT_TEXT . '</div>' : ''); ?>
                <div class="spacer5"></div>
                <div class="contact-left mgt15 w150px"><?php echo ENTRY_PASSWORD_NEW; ?></div>
                <div class="hinputbg fl"> <?php echo tep_draw_password_field('password_new', '', 'class="hinput"') ?> </div>
                <?php (tep_not_null(ENTRY_PASSWORD_NEW_TEXT) ? '<div class="required fl">' . ENTRY_PASSWORD_NEW_TEXT . '</div>' : ''); ?>
                <div class="spacer5"></div>
                <div class="contact-left mgt5 w150px"><?php echo ENTRY_PASSWORD_CONFIRMATION; ?></div>
                <div class="hinputbg fl"> <?php echo tep_draw_password_field('password_confirmation', '', 'class="hinput"') ?> </div>
                <?php (tep_not_null(ENTRY_PASSWORD_CONFIRMATION_TEXT) ? '<div class="required fl">' . ENTRY_PASSWORD_CONFIRMATION_TEXT . '</div>' : ''); ?>
                <div class="spacer5"></div>
                <div class="contact-left mgt5 w150px">&nbsp;</div>
                <div class="fl mgl10">
					<?php echo tep_submit('','class="continue-btn fl"'); ?>
                    <a href="<?=tep_href_link(FILENAME_ACCOUNT, '', 'SSL')?>" class="backbtn fr"></a>
                 </div>
                <?php (tep_not_null(ENTRY_PASSWORD_CONFIRMATION_TEXT) ? '<div class="required fl">' . ENTRY_PASSWORD_CONFIRMATION_TEXT . '</div>' : ''); ?>
                <div class="spacer5"></div>
              </div>
            </div>
            </form>
            <div class="spacer10"></div>
            <!--listing end-->
          </div>
          <!--right panel end-->
          <div class="spacer10"></div>
        </div>
      </div>
    </div>
    <div class="innermidbox-bottom"></div>
  </div>
</div>
<!--middle area end-->
<div class="spacer25"></div>
</div>
<?php
  require(DIR_WS_INCLUDES . 'template_bottom.php');
  require(DIR_WS_INCLUDES . 'application_bottom.php');
?>